package com.zy.fee.roadpark.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import com.zy.fee.roadpark.R;
import com.zy.fee.roadpark.dao.CommonLogic;
import com.zy.fee.roadpark.model.apimodel.APIM_AdminInfo;
import com.zygk.library.base.BaseActivity;
import com.zygk.library.util.ActivityCollector;
import com.zygk.library.util.HttpRequest;
import com.zygk.library.util.PreferencesHelper;
import com.zygk.library.util.Security;
import com.zygk.library.util.StringUtils;
import com.zygk.library.util.ToastUtil;

/**
 * @author: wikison
 * 登录
 * @date: 2021/12/14
 */
public class LoginActivity extends BaseActivity {
  @BindView(R.id.et_name) EditText etName;
  @BindView(R.id.et_pwd) EditText etPwd;
  @BindView(R.id.checkbox) CheckBox cbRemember;

  private String strName = "", strPwd = "";

  @Override public void setContentView() {
    setContentView(R.layout.activity_login);
  }

  @Override public void init() {
    ActivityCollector.finishAllExceptLogin(this);
  }

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ButterKnife.bind(this);

    strName = PreferencesHelper.getSettingString(PreferencesHelper.User.Key.USER_NAME);
    strPwd = PreferencesHelper.getSettingString(PreferencesHelper.User.Key.USER_PWD);

    initView();
  }

  private void initView() {
    if (PreferencesHelper.getSettingBoolean(PreferencesHelper.User.Key.USER_PWD_REMEMBER)) {
      etName.setText(strName);
      cbRemember.setChecked(true);
      etPwd.setText(strPwd);
      //login();
    } else {
      cbRemember.setChecked(false);
    }
  }

  @OnClick({
      R.id.tv_button
  }) public void onViewClicked(View view) {
    Intent intent;
    switch (view.getId()) {
      case R.id.tv_button:
        //etName.setText("15261163786");
        //etPwd.setText("1234qwer");
        strName = etName.getText().toString();
        strPwd = etPwd.getText().toString();
        if (StringUtils.isBlank(strName) || StringUtils.isBlank(strPwd)) {
          ToastUtil.showMessage(mContext, "请输入账号和密码");
          return;
        }
        //startActivity(new Intent(mContext, MainActivity.class));
        //finish();
        login();
        break;
    }
  }

  private void login() {
    CommonLogic.login(mContext, strName, Security.encryptByMD5(strPwd),
        new HttpRequest.HttpCallback() {
          @Override public void onStart() {
            showNoCancelPd();
          }

          @Override public void onSucceed(Object obj) {
            APIM_AdminInfo result = (APIM_AdminInfo) obj;
            PreferencesHelper.userManager().saveUserInfo(result.getAdminInfo());
            PreferencesHelper.userManager().saveUserID(result.getAdminInfo().getAdminID());
            preferencesHelper.setSettingString(PreferencesHelper.User.Key.USER_PWD, strPwd);
            preferencesHelper.setSettingString(PreferencesHelper.User.Key.USER_NAME, strName);
            PreferencesHelper.setSettingBoolean(PreferencesHelper.User.Key.USER_PWD_REMEMBER, true);

            Intent intent = new Intent(mContext, MainActivity.class);
            intent.putExtra(MainActivity.INTENT_UNREAD_NUM, result.getUnReadNum());
            intent.putExtra(MainActivity.INTENT_LOGIN_NAME, strName);
            startActivity(intent);
            finish();
          }

          @Override public void onFailed() {

          }

          @Override public void onFinish() {
            dismissPd();
          }
        });
  }
}
